package com.house.web.controller.system;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.house.common.core.controller.BaseController;
import com.house.common.core.domain.AjaxResult;
import com.house.common.core.domain.entity.House;
import com.house.common.core.domain.entity.Role;
import com.house.common.core.domain.entity.User;
import com.house.common.core.domain.model.LoginUser;
import com.house.common.core.page.TableDataInfo;
import com.house.common.exception.ServiceException;
import com.house.common.exception.user.UserException;
import com.house.common.utils.StringUtils;
import com.house.system.service.IHouseService;
import com.house.system.service.IUserService;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.house.system.domain.Contract;
import com.house.system.service.IContractService;

/**
 * 合同信息Controller
 *
 * @author tc
 * @date 2022-02-04
 */
@RestController
@RequestMapping("/domain/contract")
public class ContractController extends BaseController {
    @Autowired
    private IContractService contractService;

    @Autowired
    private IUserService userService;

    @Autowired
    private IHouseService houseService;


    /**
     * 查询合同信息列表
     */
    @PreAuthorize("@ss.hasPermi('domain:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(Contract contract) {
        List<Contract> list = contractService.selectContractList(contract);
        return getDataTable(list);
    }

    /**
     * 获取合同信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('domain:contract:query')")
    @GetMapping(value = "/{contractId}")
    public AjaxResult getInfo(@PathVariable("contractId") Long contractId) {
        return AjaxResult.success(contractService.selectContractByContractId(contractId));
    }

    /**
     * 新增合同信息
     */
    @PreAuthorize("@ss.hasPermi('domain:contract:add')")
    @PostMapping
    public AjaxResult add(@RequestBody Contract contract) {
        return toAjax(contractService.insertContract(contract));
    }

    /**
     * 修改合同信息
     */
    @PreAuthorize("@ss.hasPermi('domain:contract:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody Contract contract) {
        Long userId = getLoginUser().getUserId();
        contract.setaUserId(userId);
        return toAjax(contractService.updateContract(contract));
    }

    /**
     * 删除合同信息
     */
    @PreAuthorize("@ss.hasPermi('domain:contract:remove')")
    @DeleteMapping("/{contractIds}")
    public AjaxResult remove(@PathVariable Long[] contractIds) {
        return toAjax(contractService.deleteContractByContractIds(contractIds));
    }

    /**
     * 用户申请租房
     */
    @PreAuthorize("@ss.hasPermi('domain:contract:add')")
    @PostMapping("/AppContract/{houseId}")
    public AjaxResult appContract(@PathVariable("houseId") Long houseId) {
        AjaxResult ajax = AjaxResult.success();
        //从数据库获取用户个人证件信息,判断用户是否上传了个人证件
        Long userId = getLoginUser().getUserId();
        User user = userService.selectUserByUserId(userId);
        //验证用户是否上传了个人证件以及填写了个人邮箱
        if (StringUtils.isNotNull(user.getDocuments()) && StringUtils.isNotEmpty(user.getDocuments())
                && StringUtils.isNotNull(user.getEmail()) && StringUtils.isNotEmpty(user.getEmail())) {
            //再判断是否重复提交申请
            Contract contract = new Contract();
            contract.setbUserId(userId);
            contract.setHouseId(houseId);
            contract.setStatus("2");  //待处理
            if (contractService.selectContractList(contract).size() > 0) {
                //说明申请过,不需要重复申请
                throw new ServiceException("请勿重复提交,等待中介生成合同", 400);
            } else {
                contractService.userContractApp(contract);
            }
        } else {
            throw new ServiceException("个人信息尚不完善,请先完善个人信息!!!", 400);
        }
        return ajax;
    }

    /**
     * 查询用户信息
     */
    @PreAuthorize("@ss.hasPermi('domain:contract:query')")
    @GetMapping("/userDetail/{userId}")
    public AjaxResult userDetail(@PathVariable("userId") Long userId) {
        AjaxResult ajax = AjaxResult.success();
        User user = userService.selectUserByUserId(userId);
        ajax.put("user", user);
        return ajax;
    }

    /**
     * 查询房屋信息
     */
    @PreAuthorize("@ss.hasPermi('domain:contract:query')")
    @GetMapping("/houseDetail/{houseId}")
    public AjaxResult houseDetail(@PathVariable("houseId") Long houseId) {
        AjaxResult ajax = AjaxResult.success();
        House house = houseService.selectHouseByHouseId(houseId);
        ajax.put("house", house);
        return ajax;
    }
}
